import java.util.*;

public class TestList {
   public static void main( String ags[] ) {
      Random rdm = new Random();
      MyList<Member> list = new MyList<Member>() ;
      MyStack<Member> stk = new MyStack<Member>() ;
      String  msg = "Enter a command (I(nsert), R(emove), L(ist), H(elp), E(xit) ";
      char    ans;
      int     pos;
      Member  m;

      while ( true ) {
	ans = ScreenIO.promptForChar( msg ); 
	switch (  ans ) {
	    case 'I': case 'i' : 
		m	=  new Member ();
		pos 	=  rdm.nextInt( list.size() + 1 );
		list.insert( m, pos ); 
		stk.push( m );
		System.out.printf( "%s is inserted to position %d\n", m, pos );
		break;
	    case 'R' :   case 'r' : 
		pos 	= ScreenIO.promptForInt( "Enter a position : " );
		m 	= list.remove( pos );
		if ( m == null ) 
			System.out.printf("No object at position %d\n", pos );
		else
			System.out.printf("%s at position %d is rmeoved.\n", m, pos );
	        break;

	    case 'L': 
		listIterm( list, true ) ;
		break;
	    case 'l': 
		listIterm( list, false ) ;
		break;

	    case 'S': case 's' :
		listIterm( stk, true );

	    case 'H': case 'h': case '?':
		displayMenu();	break;
	    case 'E': case 'e': 
		System.exit(0);
	    
	}
      }
   }

   static void listIterm(  MyList<Member> lst , boolean dir) {
	Iterator  itr = lst.iterator(dir) ;
        while ( itr.hasNext() )  System.out.println( itr.next() );
   }

   static void displayMenu ( ) {
	System.out.println( "\t\t+========================================");
	System.out.println( "\t\t|      Doubly Linked List Structures    |");
	System.out.println( "\t\t+----------------------------------------");
	System.out.println( "\t\t+ I : Insert a random member at rand pos|");
	System.out.println( "\t\t+----------------------------------------");
	System.out.println( "\t\t+ R : Remove an item at specified pos.  |");
	System.out.println( "\t\t+----------------------------------------");
	System.out.println( "\t\t+ L : List items in the list            |");
	System.out.println( "\t\t+----------------------------------------");
	System.out.println( "\t\t+ H/? Help menu                         |");
	System.out.println( "\t\t+----------------------------------------");
	System.out.println( "\t\t+ E:  Exit this program                 |");
	System.out.println( "\t\t+----------------------------------------");
   }
}